package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.dao.BlogTagDao;
import com.have.blog.admin.domain.dto.BlogTagDto;
import com.have.blog.admin.service.AdminBlogTagService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.BlogMapper;
import com.have.blog.model.mapper.TagMapper;
import com.have.blog.model.models.Tag;
import com.have.blog.model.models.TagExample;
import com.have.blog.utils.MyBeanUtils;
import com.have.blog.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminBlogTagServiceImpl implements AdminBlogTagService {
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogTagDao tagDao;

    @Override
    public List<BlogTagDto> getList(BlogTagDto blogTagDto) {
        PageHelper.startPage(blogTagDto.getPageNum(), blogTagDto.getPageSize());
        TagExample example = new TagExample();
        TagExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (StringUtils.isNotEmpty(blogTagDto.getKeyword()) && StringUtils.isNotEmpty(blogTagDto.getKeyword().trim())) {
            criteria.andContentLike(blogTagDto.getKeyword());
        }
        if(StringUtils.isNotEmpty(blogTagDto.getOrderByAscColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(blogTagDto.getOrderByAscColumn())).toString();
            example.setOrderByClause(column + " asc");
        } else if(StringUtils.isNotEmpty(blogTagDto.getOrderByDescColumn())) {
            // 将驼峰转换成下划线
            String column = StringUtils.underLine(new StringBuffer(blogTagDto.getOrderByDescColumn())).toString();
            example.setOrderByClause(column + " desc");
        } else {
            example.setOrderByClause("sort desc");
        }
        List<Tag> tagList = tagMapper.selectByExample(example);
        List<BlogTagDto> resultList = MyBeanUtils.copyListProperties(tagList, BlogTagDto::new);
        return resultList;
    }


    @Override
    public Wrapper addTag(BlogTagDto blogTagDto) {
        String content = blogTagDto.getContent();
        TagExample example = new TagExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andContentEqualTo(content);
        List<Tag> tagList = tagMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tagList)) {
            return Wrapper.failed(AdminMessageConstant.ENTITY_EXIST);
        }
        Tag tag = new Tag();
        MyBeanUtils.copyProperties(blogTagDto, tag);
        tag.setStatus(BlogStatus.ENABLE.getStatus());
        int insert = tagMapper.insert(tag);
        if (insert > 0) {
            deleteRedisBlogTagList();
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
    }

    @Override
    public Wrapper editTag(BlogTagDto blogTagDto) {
        Tag Tag = tagMapper.selectByPrimaryKey(blogTagDto.getUid());
        if (Tag == null) {
            return Wrapper.illegal();
        }
        Tag.setContent(blogTagDto.getContent());
        Tag.setSort(blogTagDto.getSort());
        int update = tagMapper.updateByPrimaryKey(Tag);
        if (update > 0) {
            deleteRedisBlogTagList();
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
    }

    @Override
    public Wrapper deleteBatchTag(List<Integer> uids) {
        if (CollectionUtils.isEmpty(uids)) {
            return Wrapper.illegal();
        }
        TagExample example = new TagExample();
        example.createCriteria()
                .andUidIn(uids);
        Tag Tag = new Tag();
        Tag.setStatus(BlogStatus.DISABLE.getStatus());
        int delete = tagMapper.updateByExampleSelective(Tag, example);
        if (delete > 0) {
            deleteRedisBlogTagList();
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }


    @Override
    public Wrapper stickTag(BlogTagDto blogTagDto) {
        PageHelper.startPage(1, 1);
        Tag Tag = tagMapper.selectByPrimaryKey(blogTagDto.getUid());
        TagExample example = new TagExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        example.setOrderByClause("sort desc");
        List<Tag> tagList = tagMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(tagList)) {
            return Wrapper.success();
        }
        Tag maxTag = tagList.get(0);
        Tag.setSort(maxTag.getSort() + 1);
        int update = tagMapper.updateByPrimaryKey(Tag);
        if (update > 0) {
            deleteRedisBlogTagList();
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Transactional
    @Override
    public Wrapper sortTagByClickCount() {
        int count = tagDao.sortTagByClickCount();
        if (count > 0) {
            deleteRedisBlogTagList();
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    @Transactional
    @Override
    public Wrapper sortTagByBlogCount() {
        int count = tagDao.sortTagByBlogCount();
        if (count > 0) {
            // 删除Redis中的BLOG_TAG
            deleteRedisBlogTagList();
            return Wrapper.success();
        }
        return Wrapper.failed();
    }

    /**
     * TODO 删除redis中的友链连接
     */
    private void deleteRedisBlogTagList() {
    }

}
